package org.jeecg.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	
	/**
	 * 名称：long2StrDate 功能：该函数将8位或12位或14位的数值型日期时间转换为10位或16位或19带格式的日期时间 型如：
	 * 20040101 转换为 2004/01/01 或 200401010101 转换为 2004/01/01 01:01 或
	 * 20040101010101 转换为 2004/01/01 01:01:01 输入参数：Long date 十二位或十四位的数字日期时间
	 * 返回参数：String fmDate 16位或19位带格式的日期
	 */
	public static String long2StrDate(String date) {
		if (date == null || date.equals(""))
			return "";
		else
			return long2StrDate(Long.valueOf(date));
	}

	public static String int2StrDate(Integer date) {
		if (date == null)
			return "";
		return long2StrDate(Long.valueOf(date));
	}

	public static String long2StrDate(Number date) {
		String fmDate = new String();
		if (date == null) {
			fmDate = "";
		} else {
			fmDate = "";
			String year = "";
			String month = "";
			String day = "";
			String hour = "";
			String minute = "";
			// 日期格式不合法则转化为空串
			if (date.toString().length() < 6) {
				fmDate = "";
			}
			if (date.toString().length() >= 6) {
				year = (date.toString()).substring(0, 4);
				month = (date.toString()).substring(4, 6);
				fmDate = year + "-" + month;
			}
			if (date.toString().length() >= 8) {
				day = (date.toString()).substring(6, 8);
				fmDate = fmDate + "-" + day;
			}
			if (date.toString().length() >= 12) {
				hour = (date.toString()).substring(8, 10);
				minute = (date.toString()).substring(10, 12);
				fmDate = fmDate + " " + hour + ":" + minute;
			}
			if (date.toString().length() == 14) {
				fmDate = fmDate + ":" + (date.toString()).substring(12, 14);
			}
		}
		return fmDate;
	}

	public static String int2StrOnlyDate(Number date) {
		String fmDate = new String();
		if (date == null) {
			fmDate = "";
		} else {
			String year = "";
			String month = "";
			String day = "";
			fmDate = date.toString();
			if (fmDate.length() < 8) {
				fmDate = "";
			}
			if (fmDate.length() >= 8) {
				year = fmDate.substring(0, 4);
				month = fmDate.substring(4, 6);
				day = fmDate.substring(6, 8);
				fmDate = year + "-" + month + "-" + day;
			}
		}
		return fmDate;
	}

	public static String int2StrCnDate(Integer date) {
		if (date == null)
			return "";
		return long2StrCnDate(Long.valueOf(date));
	}

	public static String long2StrCnDate(Long date) {
		String fmDate = new String();
		if (date == null) {
			fmDate = "";
		} else {
			fmDate = date.toString();
			String year = "";
			String month = "";
			String day = "";
			String hour = "";
			String minute = "";
			// 日期格式不合法则转化为空串
			if (fmDate.length() < 6) {
				fmDate = "";
			}
			if ((date.toString()).length() >= 6) {
				year = (date.toString()).substring(0, 4);
				month = (date.toString()).substring(4, 6);
				fmDate = year + "年" + month + "月";
			}
			if ((date.toString()).length() >= 8) {
				day = (date.toString()).substring(6, 8);
				fmDate = fmDate + day + "日";
			}
			if ((date.toString()).length() >= 10) {
				hour = (date.toString()).substring(8, 10);
				fmDate = fmDate + hour + "时";
			}

			if ((date.toString()).length() >= 12) {
				minute = (date.toString()).substring(10, 12);
				fmDate = fmDate + minute + "分";
			}

			if ((date.toString()).length() == 14) {
				fmDate = fmDate + (date.toString()).substring(12, 14) + "秒";
			}
		}
		return fmDate;
	}

	public static String long2StrCnDate(Number date) {
		return long2StrCnDate(date.longValue());
	}

	public static String long2StrCnDate(Number date, int length) {
		if (date != null) {
			Long l = Long.valueOf(String.valueOf(date.longValue()).substring(0,
					length));
			return long2StrCnDate(l);
		}
		return null;
	}

	/**
	 * 名称：str2LongDate 功能：该函数将10位或16位或19位带格式的日期时间转换为8位或12位或14位的数值型日期时间
	 * 型如：2004/01/01 转换为 20040101 或 2004/01/01 01:01 转换为 200401010101 或
	 * 2004/01/01 01:01:01 转换为 20040101010101 如果 16位或19位带格式的日期时间 非法 ，则返回 0
	 * 输入参数：String strFmDate 16位或19位带格式的日期时间 返回参数：Long longDate 12位或14位的数值型日期时间
	 */
	public static Integer str2IntDate(String strFmDate) {
		Long date = str2LongDate(strFmDate);
		if (date == null)
			return null;
		return Integer.valueOf(date.intValue());
	}

	public static Long str2LongDate(String strFmDate) {
		if (strFmDate == null || strFmDate.length() == 0) {
			return null;
		}
		Long longDate = new Long(0);
		if (strFmDate.matches("^([12]\\d{3}-[01]\\d-[0123]\\d)$")) {
			longDate = Long.valueOf(strFmDate.replaceAll("-", ""));
		}
		if (strFmDate
				.matches("^([12]\\d{3}-[01]\\d-[0123]\\d\\s[012]\\d:[0-5]\\d)$")) {
			strFmDate = strFmDate.replaceAll("-", "").replaceAll(":", "")
					.replaceAll("\\s", "");
			longDate = Long.valueOf(strFmDate);
		}
		if (strFmDate
				.matches("^([12]\\d{3}-[01]\\d-[0123]\\d\\s[012]\\d:[0-5]\\d:[0-5]\\d)$")) {
			strFmDate = strFmDate.replaceAll("-", "").replaceAll(":", "")
					.replaceAll("\\s", "");
			longDate = Long.valueOf(strFmDate);
		}
		return longDate;
	}

	/**
	 * 编写人： 名称：getCurrentDateView 功能：得到系统当前时间显示，格式：yyyy/mm/dd主要是为页面显示用 输入参数：
	 * 返回参数：String:系统当前时间
	 */
	public static String getCurrentDateView() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		// 得出当天日期的字符串
		String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate;
		return StrCurrentCalendar;
	}

	/**
	 * 编写人： 名称：getCurrentDateTimeView 功能：得到系统当前时间显示，格式：yyyy/mm/dd hh:mm主要是为页面显示用
	 * 输入参数： 返回参数：String:系统当前时间
	 */
	public static String getCurrentDateTimeView() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
		String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
		String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
		String strSecond = String.valueOf(cldCurrent.get(Calendar.SECOND));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		if (srtHours.length() < 2) {
			srtHours = "0" + srtHours;
		}
		if (strMinute.length() < 2) {
			strMinute = "0" + strMinute;
		}
		if (strSecond.length() < 2) {
			strSecond = "0" + strSecond;
		}
		// 得出当天日期时间的字符串
		String StrCurrentCalendar = strYear + "/" + strMonth + "/" + strDate
				+ " " + srtHours + ":" + strMinute + ":" + strSecond;
		return StrCurrentCalendar;
	}

	public static Integer getIntCurrDate() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		return Integer.valueOf(strYear + strMonth + strDate);
	}

	public static Integer getIntCurrYear() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		return Integer.valueOf(strYear);
	}

	public static Long getLongCurrDateTime() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
		String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
		String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		if (srtHours.length() < 2) {
			srtHours = "0" + srtHours;
		}
		if (strMinute.length() < 2) {
			strMinute = "0" + strMinute;
		}
		return Long
				.valueOf(strYear + strMonth + strDate + srtHours + strMinute);
	}

	public static Long getLongCurrDateTime8() {
		// 获得当前日期
		Calendar cldCurrent = Calendar.getInstance();
		// 获得年月日
		String strYear = String.valueOf(cldCurrent.get(Calendar.YEAR));
		String strMonth = String.valueOf(cldCurrent.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(cldCurrent.get(Calendar.DATE));
		String srtHours = String.valueOf(cldCurrent.get(Calendar.HOUR_OF_DAY));
		String strMinute = String.valueOf(cldCurrent.get(Calendar.MINUTE));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		if (srtHours.length() < 2) {
			srtHours = "0" + srtHours;
		}
		if (strMinute.length() < 2) {
			strMinute = "0" + strMinute;
		}
		return Long.valueOf(strYear + strMonth + strDate);
	}

	/**
	 * 作者：程伟平 创建时间：Feb 29, 2008
	 * <p>
	 * 
	 * 获得系统当前时间。
	 * 
	 * @return String 日期格式：yyyyMMddHHmm 示例：200802261310
	 */
	public static String getCurrentDateTime() {
		return getDateTime(0, 0);
	}

	/**
	 * 作者：程伟平 创建时间：Feb 26, 2008
	 * <p>
	 * 
	 * 获得相对于系统当前时间的前（后）month个月、前（后）date个日的时间。
	 * 说明：当month的值为负时，为当前时间的前month个月，当month的值为正时，为当前时间的后month个月；当date的值为负时，为当前时间的前date个日，当date的值为正时，为当前时间的后date个日。
	 * 
	 * @param month
	 *            前（后）个月
	 * @param date
	 *            前（后）个日
	 * @return String 日期格式：yyyyMMddHHmm 示例：200802261310
	 */
	public static String getDateTime(int month, int date) {
		// 获得当前日期
		Calendar calendar = Calendar.getInstance();
		if (month != 0) {
			calendar.add(Calendar.MONTH, month);
		}
		if (date != 0) {
			calendar.add(Calendar.DATE, date);
		}
		// 获得年月日
		String strYear = String.valueOf(calendar.get(Calendar.YEAR));
		String strMonth = String.valueOf(calendar.get(Calendar.MONTH) + 1);
		String strDate = String.valueOf(calendar.get(Calendar.DATE));
		String srtHours = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
		String strMinute = String.valueOf(calendar.get(Calendar.MINUTE));
		// 整理格式
		if (strMonth.length() < 2) {
			strMonth = "0" + strMonth;
		}
		if (strDate.length() < 2) {
			strDate = "0" + strDate;
		}
		if (srtHours.length() < 2) {
			srtHours = "0" + srtHours;
		}
		if (strMinute.length() < 2) {
			strMinute = "0" + strMinute;
		}
		return strYear + strMonth + strDate + srtHours + strMinute;
	}

	/**
	 * 作者：程伟平 创建时间：Nov 8, 2008
	 * <p>
	 * 
	 * 获取当前年的第一天
	 * 
	 * @return String
	 */
	public static String getCurrentYearFirstDate() {

		// 获得当前日期
		Calendar calendar = Calendar.getInstance();

		String strYear = String.valueOf(calendar.get(Calendar.YEAR));

		return strYear + "01" + "01" + "00" + "00";

	}

	public static Long getLongCurrDateTime14() {
		return Long.valueOf(formateCalendar(Calendar.getInstance(),
				"yyyyMMddHHmmss"));
	}

	public static Long getLongCurrDateTime12() {
		return Long.valueOf(formateCalendar(Calendar.getInstance(),
				"yyyyMMddHHmm"));
	}

	public static String transferDate(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		return dateFormate.format(date);
	}
	public static String formatDate(Date date, String dateStr) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(dateStr);
		return dateFormate.format(date);
	}
	public static String formatDateSimple(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(
				"yyyy-MM-dd");
		return dateFormate.format(date);
	}
	public static String formatDateSimpleyyyyMMdd(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyyMMdd");
		return dateFormate.format(date);
	}
	
	public static String formatDateSimpleyyyyMMddHHmmss(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateFormate.format(date);
	}
	

	public static Date transDateSimpleyyyyMMdd(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyyMMdd");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date transDate(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy/MM/dd");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	public static Date transDateYYYY_MM_DDHHmmss(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date transDateHHmmss(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("HH:mm:ss");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	
	public static Date transDateyyyyMM(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date transDateyyMMdd(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyMMdd");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date transDateyy_MM_dd(String dateStr) {
		Date date = null;
		if (dateStr == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date= dateFormate.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	public static String formatDateSimpleMMdd(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(
				"MM-dd");
		return dateFormate.format(date);
	}

	public static String formatDateSimpleYYMM(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(
				"yyyy-MM");
		return dateFormate.format(date);
	}
	
	public static Long transferDateToLong(Date date) {
		if (date == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat("yyyyMMddHHmmss");
		return Long.valueOf(dateFormate.format(date));
	}
	

	/**
	 * yyyy/MM/dd HH:mm:ss(yyyy年，MM月，dd日，HH时，mm分，ss秒)
	 * 
	 * @param calendar
	 * @param format
	 * @return
	 */
	public static String formateCalendar(Calendar calendar, String format) {
		if (calendar == null)
			return null;
		SimpleDateFormat dateFormate = new SimpleDateFormat(format);
		return dateFormate.format(calendar.getTime());
	}

	/**
	 * 将形如20070730形式的日期格式，转换成2007-07-30
	 */
	public static String dateFormateStr(String strDate) {
		if (strDate == null)
			return "";
		String fmDate = "";
		String year = "";
		String month = "";
		String day = "";
		if (strDate.length() >= 8) {
			year = strDate.substring(0, 4);
			month = strDate.substring(4, 6);
			day = strDate.substring(6, 8);
			fmDate = year + "-" + month + "-" + day;
		}
		return fmDate;
	}

	public static Calendar long2Calendar(Long time) {
		Calendar calendar = null;
		if (time == null) {
			calendar = Calendar.getInstance();
		} else {
			String strTime = String.valueOf(time);
			int year = 0;
			int month = 0;
			int day = 0;
			int hour = 0;
			int minute = 0;
			int sec = 0;
			if (strTime.length() < 8) {

			}
			if (strTime.length() >= 8) {
				year = Integer.valueOf(strTime.substring(0, 4));
				month = Integer.valueOf(strTime.substring(4, 6)) - 1;
				day = Integer.valueOf(strTime.substring(6, 8));
			}
			if (strTime.length() >= 10) {
				hour = Integer.valueOf(strTime.substring(8, 10));
			}
			if (strTime.length() >= 12) {
				minute = Integer.valueOf(strTime.substring(10, 12));
			}
			if (strTime.length() >= 14) {
				sec = Integer.valueOf(strTime.substring(12, 14));
			}
			calendar = new GregorianCalendar(year, month, day, hour, minute,
					sec);
		}
		return calendar;
	}

	/**
	 * 计算时间添加天数计算，给添加评标活动时使用，用于计算评标截止时间 问题编号：[fix-005]
	 * 
	 * @param time
	 *            原来的时间
	 * @param days
	 *            加的天加1天 1.5一天半
	 * @return
	 */
	public static long calcTimeAddOfDay(long time, double days) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");

		Date date1 = new Date();
		try {
			date1 = format.parse(String.valueOf(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int addDay = (int) (days * 24);
		long Time = (date1.getTime() / 1000) + (60 * 60 * addDay);

		date1.setTime(Time * 1000);

		String mydate = format.format(date1);

		return Long.parseLong(mydate);
	}

	/**
	 * 时间添加天数计算 Author 张华 Jul 31, 2009
	 * 
	 * @param time
	 *            原来的时间(如：20090731)
	 * @param days
	 *            加的天加1天 1.5一天半
	 * @return
	 */
	public static int calcTimeAddOfDay(int time, double days) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

		Date date1 = new Date();
		try {
			date1 = format.parse(String.valueOf(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int addDay = (int) (days * 24);
		long Time = (date1.getTime() / 1000) + (60 * 60 * addDay);

		date1.setTime(Time * 1000);

		String mydate = format.format(date1);

		return Integer.parseInt(mydate);
	}

	/**
	 * 功能描述：得到给定时间减去给定天数后的时间<br>
	 * 
	 * @param time
	 *            时间
	 * @param days
	 *            天数
	 * @return long <BR>
	 *         作者：杨凯<BR>
	 *         时间：Jun 17, 2008 <br>
	 */
	public static long getTimeSubtractOfDay(long time, int days) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

		Date date1 = new Date();
		try {
			date1 = format.parse(String.valueOf(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int subtractDay = (int) (days * 24);
		long Time = (date1.getTime() / 1000) - (60 * 60 * subtractDay);

		date1.setTime(Time * 1000);

		String mydate = format.format(date1);

		return Long.parseLong(mydate);
	}

	/**
	 * 功能描述：得到当前时间之前或之后的数据<br>
	 * 
	 * @param number
	 *            正数为以后时间 负数为之前时间 type 1 年 2 月 3 日 ws 返回的长度
	 * @return long <BR>
	 *         作者：陈伟<BR>
	 *         时间：Jun 17, 2008 <br>
	 */
	public static long getDateToNow(int number, int type, int ws) {
		Long resultLong = null;
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		System.out.println(cal.getTime().toLocaleString());
		if (type == 1) {
			cal.add(cal.YEAR, number);
		} else if (type == 2) {
			cal.add(cal.MONTH, number);
		} else if (type == 3) {
			cal.add(cal.DAY_OF_MONTH, number);
		}
		resultLong = transferDateToLong(cal.getTime());
		if (ws == 8) {
			resultLong = resultLong / (1000000);
		}
		return resultLong;
	}
	
	/**
	 * 得到当前时间之前或之后的数据，返回日期格式 yyyy-MM-dd
	 * @param number 正数为以后时间 负数为之前时间
	 * @param type 1 年 2 月 3 日
	 * @return
	 */
	public static String getDateToNow(int number, int type) {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		
		if (type == 1) {
			cal.add(cal.YEAR, number);
		} else if (type == 2) {
			cal.add(cal.MONTH, number);
		} else if (type == 3) {
			cal.add(cal.DAY_OF_MONTH, number);
		}
		
		String str = formatDateSimple(cal.getTime());
		return str;
	}

	/**
	 * 取得月份的第一天
	 * 
	 * @param
	 *
	 * @return String
	 */
	public static Long getMonthBegin(Long time) {
		Calendar cal = long2Calendar(time);
		int year = cal.get(cal.YEAR);
		int month = cal.get(cal.MONTH);
		cal.set(year, month, 1);
		return calendarToLong8(cal);
	}

	/**
	 * 转化日历为长整形
	 * 
	 * @param cal
	 * @return
	 */
	public static Long calendarToLong8(Calendar cal) {
		int year = cal.get(cal.YEAR);
		int month = cal.get(cal.MONTH) + 1;
		int date = cal.get(cal.DATE);
		String strMonth = month < 10 ? ("0" + month) : String.valueOf(month);
		String strDate = date < 10 ? ("0" + date) : String.valueOf(date);

		return Long.parseLong(year + strMonth + strDate);
	}

	/**
	 * 取得指定月份的最后一天
	 * 
	 * @param
	 *
	 * @return String
	 */
	public static Long getMonthEnd(Long time) {
		Calendar cal = long2Calendar(getMonthBegin(time));
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		int yeah = cal.get(cal.YEAR);
		int month = cal.get(cal.MONTH);
		int day = cal.get(cal.DATE);
		return calendarToLong8(cal);
	}

	/**
	 * 获取指定日期date的星期dayOfWeek Author 张华 Aug 3, 2009
	 * 
	 * @param date
	 *            日期如:20090803
	 * @param dayOfWeek
	 *            周几(0-周日,1-周一...6-周六)
	 * @return
	 */
	public static Integer getIntDayOfWeek(int date, int dayOfWeek) {
		Date date2 = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try {
			date2 = format.parse(String.valueOf(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date2);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek + 1);
		return Integer.valueOf(dateFormat.format((calendar.getTime())));
	}

	/**
	 * 获得当前周的“星期几” Author 张华 Jul 2, 2009
	 * 
	 * @param dayOfWeek
	 *            周几(从2开始),请使用常量(如：Calendar.MONDAY)
	 * @return 返回八位的整型数字(如：20090629)
	 */
	public static Integer getIntDayOfWeek(int dayOfWeek) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
		return Integer.valueOf(dateFormat.format((calendar.getTime())));
	}
	
	public static Integer getIntDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		int days = calendar.get(calendar.DAY_OF_MONTH);
		return days;
	}
	
	

	/**
	 * 计算此日期是一周中的第几天 Author 张华 Jul 30, 2009
	 * 
	 * @param date
	 *            长整型日期(如:20090730)
	 * @return
	 */
	public static Integer getDayOfWeek(Long date) {
		Calendar calendar = long2Calendar(date);
		return calendar.get(Calendar.DAY_OF_WEEK) - 1 == 0 ? 7 : calendar
				.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 获得指定日期是星期几 Author 张华 Jul 30, 2009
	 * 
	 * @param date
	 *            长整型日期(如:20090730)
	 * @return 星期几(如:星期四)
	 */



	/**
	 * 计算月份的加减 Author 张华 Aug 3, 2009
	 * 
	 * @param date
	 *            起始日期(如:20090801)
	 * @param addMonth
	 *            待加的月数(如:1)
	 * @return 返回形如:20090901
	 */
	public static int calcTimeAddOfMonth(int date, int addMonth) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date dateTmp = new Date();
		try {
			dateTmp = format.parse(String.valueOf(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);

		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + addMonth);

		return Integer.parseInt(format.format(calendar.getTime()));
	}

	/**
	 * 计算分钟的加减 Author 张华 Aug 3, 2009
	 * 
	 * @param date
	 *            起始日期(如:20090801033330)
	 * @param
	 *
	 * @return 返回形如:20090801033830
	 */
	public static long calcTimeAddOfMin(long date, int min) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date dateTmp = new Date();
		try {
			dateTmp = format.parse(String.valueOf(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);

		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + min);

		return Long.valueOf(format.format(calendar.getTime()));
	}

	/**
	 * 计算月份的加减 Author 张华 Aug 3, 2009
	 * 
	 * @param date
	 *            起始日期(如:20090801)

	 * @return 返回形如:20090701
	 */
	public static int calcTimeCutOfMonth(int date, int cutMonth) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date dateTmp = new Date();
		try {
			dateTmp = format.parse(String.valueOf(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);

		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - cutMonth);

		return Integer.parseInt(format.format(calendar.getTime()));
	}

	/**
	 * 计算年份的加减 王东波 2011.7.6
	 * 
	 * @param date
	 *            起始日期(如:20090801)

	 * @return 返回形如:2010801
	 */
	public static int calcTimeAddOfYear(int date, int addYear) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date dateTmp = new Date();
		try {
			dateTmp = format.parse(String.valueOf(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTmp);

		calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + addYear);

		return Integer.parseInt(format.format(calendar.getTime()));
	}



	/**
	 * Author 王东波
	 * 
	 * @param
	 * @return 返回形如:10：10
	 */
	public static String convertTime(String time) {

		String result = "";
		if (time == null || "".equals(time)) {
			result = "";
		}
		if (time.length() == 3) {
			result = time.substring(0, 1) + ":" + time.substring(1, 3);
		} else {
			result = time.substring(0, 2) + ":" + time.substring(2, 4);
		}
		return result;
	}

	/**
	 * @author 顾士孔
	 * @date 2011-6-2
	 * @discription : 20090104165030时间格式转换成16时50分30秒的格式
	 * @param date
	 * @return String
	 */
	public static String getLongTime(Number date) {
		String fmDate = new String();
		if (date == null) {
			fmDate = "";
		} else {
			fmDate = String.valueOf(date.longValue());
			String hour = "";
			String minute = "";
			String seconds = "";
			// 日期格式不合法则转化为空串
			if (fmDate.length() < 14) {
				fmDate = "";
			}
			if (fmDate.length() == 14) {
				hour = fmDate.substring(8, 10);
				minute = fmDate.substring(10, 12);
				seconds = fmDate.substring(12, 14);
				fmDate = hour + ":" + minute + ":" + seconds;
			}
		}
		return fmDate;
	}

	// 返回当前时间前days天的时间，days小于28，cur_time必须大于8位
	public static String getEarlyDays(String cur_time, int days) {
		int day = Integer.parseInt(cur_time.substring(6, 8));
		int mon = Integer.parseInt(cur_time.substring(4, 6));
		int year = Integer.parseInt(cur_time.substring(0, 4));
		int newDay = 0, newMon = 0, newYear = 0;
		String newDateString = "";
		// System.out.println(year+":"+mon+":"+day+"........................................"+cur_time);
		if (day - days < 1) {
			if (mon == 1) {
				newDay = 31 - (days - day);
				newMon = 12;
				newYear = year - 1;
			} else {
				if (mon - 1 == 1 || mon - 1 == 3 || mon - 1 == 5
						|| mon - 1 == 7 || mon - 1 == 8 || mon - 1 == 12) {
					newDay = 31 - (days - day);
					newMon = mon - 1;
					newYear = year;
				}
				if (mon - 1 == 4 || mon - 1 == 6 || mon - 1 == 9
						|| mon - 1 == 11) {
					newDay = 30 - (days - day);
					newMon = mon - 1;
					newYear = year;
				}
				if (mon - 1 == 2) {
					newDay = 28 - (days - day);
					newMon = mon - 1;
					newYear = year;
				}
			}
		} else {
			newDay = day - days;
			newMon = mon;
			newYear = year;
		}
		newDateString = "" + newYear;
		if (newMon < 10) {
			newDateString = newDateString + "0" + newMon;
		} else {
			newDateString = newDateString + newMon;
		}
		if (newDay < 10) {
			newDateString = newDateString + "0" + newDay;
		} else {
			newDateString = newDateString + newDay;
		}
		// System.out.println(newYear+":"+newMon+":"+newDay+":"+newDateString+cur_time.substring(8));
		return newDateString + cur_time.substring(8);
	}

 
	public static void getData(Date newData){
		Calendar cad = Calendar.getInstance();
		cad.setTime(newData);
		int days = cad.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println("days"+days);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		int weeks = cad.get(Calendar.WEEK_OF_MONTH);
		System.out.println("weeks"+weeks);
		int day = cad.get(Calendar.DAY_OF_WEEK);
		System.out.println("day"+day);
		for(int i=0;i<weeks;i++) {
			if(i==0) {
				for (int j = 0; j <= day; j++) {
	                System.out.println(j+"q");
	            }
			}
			
		}
		 
	}


	public static Date addDay(Date date, int i) {
		Calendar can =Calendar.getInstance();
		can.setTime(date);
		can.add(Calendar.DATE, i);
		return can.getTime();
	}
	public static Date addMin(Date date, int i) {
		Calendar can =Calendar.getInstance();
		can.setTime(date);
		can.add(Calendar.MINUTE, i);
		return can.getTime();
	}
	
	/**
	 * 计算两个日期的差
	 * @param date1 前一个日期
	 * @param date2 后一个日期
	 * @return
	 */
	public static long getDateDiff(Date date1, Date date2) {
		return date2.getTime() - date1.getTime();
	}
	

	
}
